<?
require_once '../../system/init.php';

$events = new Events();
$event = $events->editEventAction();


$photos = $events->getEventPhotosFromDb($event['event_id']);
$pager = $events->getPhotoPagerHtml($event['event_id']);

$folders = array_keys($events->getEventsFoldersFromStorage());
$eventsFolders = $events->getEventsFoldersFromDb();

$folders = array_diff($folders, $eventsFolders);

require_once '../inc/header.php';
// Страница только для залогиненных пользователей
Common::checkLogin($user);

?>

<div class="main">
    <div class="center">
        <ul class="menu">
            <li><a href="/index.php" class="btn">Альбомы</a></li>
            <li><a href="/tv/all_events.php" class="btn  btn-active">События</a></li>
        </ul>

        <div id="user-info" class="h">
            <span id="user-name"></span>
            <span id="user-lastname"></span>
        </div>
    </div>
    <div class="clear"></div>
    <fieldset><legend><h1>Редактирование события</h1></legend></fieldset>

    <div style="width: 500px; margin: 0 auto;">
        <? if ($event): ?>
        <form method="post" enctype="multipart/form-data" onsubmit="return checkForm ( );">
            <input type="hidden" name="event_id" value="<?=$event['event_id']?>" />
            <input type="hidden" name="event_folder" value="<?=$event['event_folder']?>" />
            <table class="form" cellpadding="0" cellspacing="0">
                <tr>
                    <td class="field-name"><label for="event_name">Название</label></td>
                    <td><input type="text" name="event_name" id="event_name" class="w-400" value="<?=$event['event_name']?>" onFocus="hideError(this.id);" /></td>
					<td class="errors" id="event_name_error"></td>
				</tr>
                <tr>
                    <td class="field-name"><label for="event_author">Автор</label></td>
                    <td><input type="text" name="event_author" id="event_author" class="w-400" value="<?=$event['event_author']?>"/></td>
                    <td class="errors"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="event_date">Дата</label></td>
                    <td><input type="text" name="event_date" id="event_date" class="w-400 datepicker" value="<?=$event['event_date']?>" /></td>
                     <td id="event-date-error" class="errors"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="event_desc">Описание</label></td>
                    <td><textarea id="event_desc" name="event_desc" class="w-400 h-100"><?=$event['event_desc']?></textarea></td>
                    <td class="errors" id="event-desc-error"></td>
                </tr>
                <tr>
                    <td class="field-name">Фотографии</td>
                    <td class="middle">
                        <input type="file" value="" name="photos[]" multiple>
                        <span class="relative">Папка c фото: <?=$event['event_folder']?>&nbsp;</span>
                    </td>
                </tr>
                <tr>
                    <td class="field-name"></td>
                    <td><label><input type="checkbox" id="event-rotate" name="event_rotate" <?= $event['event_rotate'] ? 'checked' : ''?> /> Ротировать</label></td>
                </tr>
                <tr>
                    <td class="field-name"></td>
                    <td><input type="submit" name="submit" class="btn" />&nbsp;&nbsp;<input type="submit" name="delete" class="btn" value="Удалить" /></td>
                </tr>
            </table>
        </form>  
    </div><br>
    <? if ($photos) :?>  
        <fieldset><legend><h1> Фотографии события </h1></legend></fieldset>
        <?=$pager?>
         <div class="event_photos">
            <? foreach ($photos as $photo): ?>
                <div class="photo photo-<?=$photo['photo_id']?>"  >
                    <a href="/<?=$photo['photo_path']?>" class="img" rel="prettyPhoto" style="background-image: url('/<?=$photo['photo_path']?>'); background-repeat: no-repeat;" title="<?=$photo['photo_desc']?>" ></a>
                    <form class="photo-info">
                        <table class="form" cellpadding="0" cellspacing="0">
                        <tr>
                            <td class="field-name"><label class="field-name" for="photo-desc-<?=$photo['photo_id']?>">Описание</label></td>
                            <td><textarea id="<?=$photo['photo_id']?>" name="photo-desc" class="w-400 h-90 photo_desc_input"><?=$photo['photo_desc']?></textarea></td>
                        </tr> 
                        <tr>
                            <td class="field-name"></td>
                            <td><label><input id="<?=$photo['photo_id']?>" class="photo_rotate_checkbox" type="checkbox" name="rotation" value="1" <? if ($photo['photo_rotate'] != 0){ echo 'checked' ;} ?>> Ротировать </label></td>
                        </tr>
                        <tr>
                            <td class="field-name"></td>
                            <td>
                            <input type="button" class="btn photo-info-save" value="Удалить" onclick="del(<?=$photo['photo_id']?>, '<?=$photo['photo_path']?>' );"/>
                            <em class="em" style="display: none; top: -85px; "></em>
                            </td>
                        </tr>
                        </table>
                    </form>
                    
                </div>
            <? endforeach ?>
         </div>
         <a class="btn btn-up" href="#"> Наверх </a>
         <?=$pager?>
         <? endif ?> 
     <? endif ?>
</div>


<script>
  $(document).ready(function(){
    $("a[rel^='prettyPhoto']").prettyPhoto();
  });
</script>



<script>


$(document).ready(function(){
        
        $('.photo_rotate_checkbox').change(function(){
            
            var id = $(this).attr('id');
            var val = $(this).attr('checked');
            
            if(val) {
                val = 1;
            } else {
                val = 0;
            }
            
            var params = {id:id, ajax:1, val:val, rotate:1};
            
            params = $.param(params);
            
                    $.post("/ajax_photoInfoEdit.php", params , function (data) {
                    if (data.message) {
                        $('.photo-' + id).find(".em").html(data.message).animate({opacity: "show", top: "-65"}, 500);
                        $('.photo-' + id).find(".em").animate({opacity: "hide", top: "-65"}, 1500);
                    }
                    }); 
          
        })
        
        $('.photo_desc_input').change(function(){
            var id = $(this).attr('id');
            var val = $(this).val();
            
            var params = {id:id, ajax:1, val:val, desc:1};
            
            params = $.param(params);
            
                    $.post("/ajax_photoInfoEdit.php", params , function (data) {
                    if (data.message) {
                        $('.photo-' + id).find(".em").html(data.message).animate({opacity: "show", top: "-65"}, 500);
                        $('.photo-' + id).find(".em").animate({opacity: "hide", top: "-65"}, 1500);
                    }
                    });
        })

               
});        


function del (id, path){
    
        var params = {id:id, ajax:1, path:path}; 
        params = $.param(params)
         
        $.post("/ajax_photoDelete.php", params , function (data) {
                    if (data.message) {
                        $('body').find('.photo-' + id).animate({ backgroundColor: "#fbc7c7" }, "fast")
        .animate({ opacity: "hide" }, "slow")
                    } else {
                        alert(data.message);
                    }
        });          
}




function checkForm() {
	var nameEvent = $("#event_name"); 
	
	if (nameEvent.val()) {
		return true;
	} else {
		nameEvent.css('border','1px #ff4040 solid');
		$("#event_name_error").text('введите название события');
		return false;
	} 
}


function hideError(inputFieldId) {
	$("#"+inputFieldId+"-error").text('');
	$("#"+inputFieldId).css('border','1px #ccc solid');
}
   
</script>

<script>
$(function(){
    $.datepicker.setDefaults(
        $.extend($.datepicker.regional["ru"])
    );
    $(".datepicker").datepicker({
        dateFormat:'yy-mm-dd'
    });
});

</script>
<? require_once '../inc/footer.php'; ?>